package com.yc.d0303;

import com.google.gson.Gson;
import com.yc.demo.bean.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "AddCartServlet", value = "/AddCartServlet")
public class AddCartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        final String name = req.getParameter("name");
        final HttpSession session = req.getSession();
        // 获取原有数量
        Integer nums = (Integer) session.getAttribute(name);
        if (nums == null){
            nums = 0;
        }
        nums ++;
        session.setAttribute(name, nums);
        Result res = new Result(1,"添加成功",null);
        final String json = new Gson().toJson(res);
        resp.getWriter().append(json);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
